#!/usr/bin/bash

SCRIPT_PATH=`realpath $0`
SCRIPT_DIR=`dirname ${SCRIPT_PATH}`

# Validate exit code of the previous process
function ret_validate {
    retVal=$?
    if [[ ${retVal} -ne 0 ]]; then
        echo "## FAILED"
        exit ${retVal}
    fi
    echo "## PASSED"
}

DEBUG=0
if [[ $2 == "debug" || $1 == "debug" ]]; then
  DEBUG=1
fi


cd ${SCRIPT_DIR}

mkdir -p ./bin

echo "## BUILDING SERVER"
if [[ ${DEBUG} == 1 ]]; then
echo "## DEBUG MODE ON"
cd cmd && go build -gcflags="all=-N -l" -o ../bin/server
else
echo "## DEBUG MODE OFF"
cd cmd && go build -o ../bin/server
fi
ret_validate
